package com.ywgt.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ywgt.response.Result;
import com.ywgt.system.pojo.*;
import com.ywgt.system.service.ProductService;
import com.ywgt.system.service.ProductStockService;
import com.ywgt.system.vo.ProductVO;
import com.ywgt.utils.IDWorker;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yiping
 * @since 2021-01-22
 */
@RestController
@Api(value = "商品模块",tags = "商品模块")
@RequestMapping("/business/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductStockService productStockService;

    @Autowired
    private IDWorker idWorker;

    @ApiOperation(value = "查找所有商品",notes ="根据条件查询所有商品" )
    @GetMapping("/findProducts")
    public Result getProducts(@RequestParam(value = "pageNum",defaultValue = "1")Long pageNum,
                              @RequestParam(value ="pageSize",defaultValue = "6")Long pageSize,
                              @RequestParam(value = "categorys",required = false)String categorys,Product product){
        Page<Product> page=new Page<>(pageNum,pageSize);
        Page<Product> productPage = productService.page(page,getWrapper(categorys,product));
        List<Product> products = productPage.getRecords();
        long total = productPage.getTotal();
        return Result.ok().data("rows",products).data("total",total);
    }

    @ApiOperation(value = "将商品放进回收站",notes ="放回回收站" )
    @PutMapping("/remove/{id}")
    public Result remove(@PathVariable("id")Long id){

        LambdaUpdateWrapper<Product> updateWrapper=new LambdaUpdateWrapper<>();
        updateWrapper.eq(Product::getId,id);
        updateWrapper.set(Product::getStatus,1);
        if (productService.update(updateWrapper)){
            return Result.ok();
        }
        return Result.error();
    }

    @ApiOperation(value = "删除商品",notes ="删除商品" )
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id")Long id){
        if (productService.removeById(id)){
            return Result.ok();
        }
        return Result.error();
    }


    @ApiOperation(value = "审核商品",notes ="审核商品" )
    @PutMapping("/publish/{id}")
    public Result publish(@PathVariable("id")Long id){

        LambdaUpdateWrapper<Product> updateWrapper=new LambdaUpdateWrapper<>();
        updateWrapper.eq(Product::getId,id);
        updateWrapper.set(Product::getStatus,0);
        if (productService.update(updateWrapper)){
            return Result.ok();
        }
        return Result.error();
    }
    @ApiOperation(value = "移出回收站",notes ="移出回收站" )
    @PutMapping("/back/{id}")
    public Result back(@PathVariable("id")Long id){
        LambdaUpdateWrapper<Product> updateWrapper=new LambdaUpdateWrapper<>();
        updateWrapper.eq(Product::getId,id);
        updateWrapper.set(Product::getStatus,0);
        if (productService.update(updateWrapper)){
            return Result.ok();
        }
        return Result.error();
    }

    @ApiOperation(value = "获取商品信息",notes ="根据id获取商品信息" )
    @GetMapping("/edit/{id}")
    public Result edit(@PathVariable("id")Long id){
        Product product = productService.getById(id);
        return Result.ok().data("row",product);
    }
    @ApiOperation(value = "更新商品",notes ="更新商品信息" )
    @PutMapping("/update/{id}")
    public Result update(@PathVariable("id")Long id, @RequestBody ProductVO productVO){
        Product product=new Product();
        BeanUtils.copyProperties(productVO,product);
        LambdaUpdateWrapper<Product> updateWrapper=new LambdaUpdateWrapper<>();
        updateWrapper.eq(Product::getId,id);
        if (productService.update(product,updateWrapper)){
            return Result.ok();
        }
        return Result.error();
    }
    @ApiOperation(value = "添加商品",notes ="添加商品" )
    @PostMapping("/add")
    public Result add(@RequestBody ProductVO productVO){
        Product product=new Product();
        BeanUtils.copyProperties(productVO,product);
        product= convertProduct(productVO, product);
        if (productService.save(product)){
            return Result.ok();
        }
        return Result.error();
    }
    @ApiOperation(value = "查询所有库存",notes ="查询所有库存" )
    @GetMapping("/findAllStocks")
    public Result findAllStocks(@RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                                @RequestParam(value = "pageSize",defaultValue = "4",required = false)Integer pageSize,
                                @RequestParam(value = "name",required = false) String name,
                                @RequestParam(value = "categorys",required = false)String categorys) {
        com.github.pagehelper.Page<Product> productPage = productService.findAllStocks(pageNum, pageSize, name, categorys);
        long total = productPage.getTotal();
        List<Product> products = productPage.getResult();
        return Result.ok().data("rows", products).data("total", total);
        }
//    @ApiOperation(value = "移出回收站",notes ="移出回收站" )
//    @GetMapping("/findProductStocks")
//    public Result findProductStocks(@RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
//                                @RequestParam(value = "pageSize",defaultValue = "4",required = false)Integer pageSize,
//                                @RequestParam(value = "name",required = false) String name,
//                                @RequestParam(value = "categorys",required = false)String categorys) {
//        com.github.pagehelper.Page<Product> productPage = productService.findAllStocks(pageNum, pageSize, name, categorys);
//        long total = productPage.getTotal();
//        List<Product> products = productPage.getResult();
//        return Result.ok().data("rows", products).data("total", total);
//    }

    public Product convertProduct(ProductVO productVO,Product product){
        product.setPNum(UUID.randomUUID().toString());
        Long[] categoryKeys = productVO.getCategoryKeys();
        switch (categoryKeys.length){
            case 1: product.setOneCategoryId(categoryKeys[0]);
                break;
            case 2: product.setOneCategoryId(categoryKeys[0]);
                product.setTwoCategoryId(categoryKeys[1]);
                break;
            case 3: product.setOneCategoryId(categoryKeys[0]);
                product.setTwoCategoryId(categoryKeys[1]);
                product.setThreeCategoryId(categoryKeys[2]);
                break;
        }
        return product;
    }





    public LambdaQueryWrapper<Product> getWrapper(String categorys,Product product){
        if (!StringUtils.isEmpty(categorys)) {
            String[] split = categorys.split(",");
            switch (split.length){
                case 1: product.setOneCategoryId(Long.parseLong(split[0]));
                        break;
                case 2: product.setOneCategoryId(Long.parseLong(split[0]));
                        product.setTwoCategoryId(Long.parseLong(split[1]));
                        break;
                case 3: product.setOneCategoryId(Long.parseLong(split[0]));
                        product.setTwoCategoryId(Long.parseLong(split[1]));
                        product.setThreeCategoryId(Long.parseLong(split[2]));
                        break;
            }
        }
        LambdaQueryWrapper<Product> queryWrapper=new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(product.getName())){
            queryWrapper.like(Product::getName,product.getName());
        }
        if (!StringUtils.isEmpty(product.getStatus())){
            queryWrapper.like(Product::getStatus,product.getStatus());
        }
        if (!StringUtils.isEmpty(product.getOneCategoryId())){
            queryWrapper.eq(Product::getOneCategoryId,product.getOneCategoryId());
        }
        if (!StringUtils.isEmpty(product.getTwoCategoryId())){
            queryWrapper.eq(Product::getTwoCategoryId,product.getTwoCategoryId());
        }
        if (!StringUtils.isEmpty(product.getThreeCategoryId())){
            queryWrapper.eq(Product::getThreeCategoryId,product.getThreeCategoryId());
        }
        return queryWrapper;
    }
}

